package org.mappland.compltres.controller.user;

import org.mappland.compltres.common.CommonResult;
import org.mappland.compltres.common.StatusCode;
import org.mappland.compltres.domain.Machine;
import org.mappland.compltres.domain.Order;
import org.mappland.compltres.domain.Type;
import org.mappland.compltres.domain.User;
import org.mappland.compltres.dto.BookDTO;
import org.mappland.compltres.dto.DateSectionDTO;
import org.mappland.compltres.dto.ReturnMachineDTO;
import org.mappland.compltres.dto.TypeDTO;
import org.mappland.compltres.service.MachineService;
import org.mappland.compltres.service.OrderService;
import org.mappland.compltres.service.TypeService;
import org.mappland.compltres.service.UserService;
import org.mappland.compltres.util.WebUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user")
public class MachineController {
    @Resource
    private MachineService machineService;
    @Resource
    private OrderService orderService;
    @Resource
    private TypeService typeService;
    @Resource
    private UserService userService;

    // 用户获取机器列表
    @PostMapping(value = "/listMachine")
    public CommonResult<List<ReturnMachineDTO>> listMachine(@RequestBody DateSectionDTO dateSectionDTO) {
        CommonResult<List<ReturnMachineDTO>> result = new CommonResult<>();
        List<ReturnMachineDTO> list = machineService.listMachines(dateSectionDTO);
        result.setCode(StatusCode.COMMON_SUCCESS.getCode());
        result.setMessage(StatusCode.COMMON_SUCCESS.getMessage());
        result.setData(list);

        return result;
    }

    // 用户获取机器详情
    @PostMapping(value = "/machineDetail")
    public CommonResult<ReturnMachineDTO> machineDetail(@RequestParam("machineId") Integer machineId) {
        CommonResult<ReturnMachineDTO> commonResult = new CommonResult<>();

        ReturnMachineDTO returnMachineDTO = machineService.machineDetail(machineId);
        commonResult.setData(returnMachineDTO);
        commonResult.setCode(StatusCode.COMMON_SUCCESS.getCode());
        commonResult.setMessage(StatusCode.COMMON_SUCCESS.getMessage());
        return commonResult;
    }

    @PostMapping("/bookMachine")
    public CommonResult<String> bookRoom(@RequestBody BookDTO bookDTO) {
        CommonResult<String> commonResult = new CommonResult<>();

        User user = (User) WebUtils.getSession().getAttribute("loginUser");
        if(user == null) {
            commonResult.setData("登录信息过期");
            commonResult.setCode(StatusCode.COMMON_FAIL.getCode());
            commonResult.setMessage(StatusCode.COMMON_FAIL.getMessage());
            return commonResult;
        }
        Machine machine = machineService.getById(bookDTO.getMachineId());
        Type type = typeService.getById(machine.getType());
        Order order = new Order();
        BeanUtils.copyProperties(bookDTO,  order);
        order.setUserId(user.getId());

        int days = (int) Math.ceil((bookDTO.getLeaveTime().getTime() - bookDTO.getInTime().getTime()) / (60 * 60 * 24 * 1000 * 1.0));

        order.setRealPrice(type.getPrice() * days);
        orderService.save(order);

        user.setState(1);
        userService.updateById(user);

        commonResult.setCode(StatusCode.COMMON_SUCCESS.getCode());
        commonResult.setMessage(StatusCode.COMMON_SUCCESS.getMessage());
        commonResult.setData("预订成功!");

        return commonResult;
    }

    @PostMapping("/listMachinesByTypeId")
    public CommonResult<List<ReturnMachineDTO>> listMachinesByTypeId(@RequestBody TypeDTO typeDTO) {
        CommonResult<List<ReturnMachineDTO>> commonResult = new CommonResult<>();

        DateSectionDTO dateSectionDTO = new DateSectionDTO();
        BeanUtils.copyProperties(typeDTO, dateSectionDTO);
        List<ReturnMachineDTO> machineList = machineService.listMachines(dateSectionDTO);

        List<ReturnMachineDTO> returnMachineList = new ArrayList<>();
        if (!machineList.isEmpty()) {
            for (ReturnMachineDTO room : machineList) {
                if (typeDTO.getTypeId().equals(room.getType().getId())) {
                    returnMachineList.add(room);
                }
            }
        }

        commonResult.setData(returnMachineList);
        commonResult.setCode(StatusCode.COMMON_SUCCESS.getCode());
        commonResult.setMessage(StatusCode.COMMON_SUCCESS.getMessage());
        return commonResult;
    }
}
